/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.wsinfra.wsa.impl;

import javax.xml.namespace.QName;

import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;

import cgl.narada.wsinfra.schemas.addressing.EndpointReferenceDocument;
import cgl.narada.wsinfra.schemas.addressing.EndpointReferenceType;
import cgl.narada.wsinfra.schemas.addressing.ReferenceParametersType;
import cgl.narada.wsinfra.schemas.addressing.ReferencePropertiesType;
import cgl.narada.wsinfra.wsa.WsaEprCreator;

/** This is a utility class which facilitates the creation of Endpoint
    References.

    @author Shrideep Pallickara
    $Date: 2005/07/29 22:41:30 $
    $Revision: 1.3 $

*/

public class WsaEprCreatorImpl implements WsaEprCreator {
  private static final WsaEprCreator instance = new WsaEprCreatorImpl();
  private String moduleName = "WsaEprCreatorImpl: ";

  private WsaEprCreatorImpl() {

  }

  public static WsaEprCreator getInstance() {
    return instance;
  }


  /** Initialize the endpoint reference type */
  private EndpointReferenceType getEndpointReferenceType() {
    EndpointReferenceDocument eprDoc =
      EndpointReferenceDocument.Factory.newInstance();
    EndpointReferenceType eprType = eprDoc.addNewEndpointReference();

    return eprType;
  }


  /** Creats an Endpoint reference type based on the specified address */
  public EndpointReferenceType createEpr(String address) {
    EndpointReferenceType eprType = getEndpointReferenceType();
    if (address == null) {
      System.out.println(moduleName + "Specified Address is NULL!");
    }
    eprType.addNewAddress().setStringValue(address);
    return eprType;
  }


  /** Creats an Endpoint reference type based on the specified address,
      portType and ServiceName*/
  public EndpointReferenceType createEpr(String address,
					 String portType, String serviceName) {
    EndpointReferenceType eprType = createEpr(address);
    if (portType != null) {
      eprType.addNewPortType().setStringValue(portType);
    }

    if (serviceName != null) {
      eprType.addNewServiceName().setPortName(serviceName);
    }

    return eprType;
  }


  /** Adds an attribute to the ReferenceProperties type within the specified
      Endpoint reference. If type does not contain the ReferenceProperties
      element, a new one is created. Then the specified attribute is added to
      the ReferenceProperties*/
  public void addReferencePropertiesAttribute(EndpointReferenceType eprType,
					      QName qname, String value) {
    if (qname == null || value == null) {
      reportOnQNameAndValue(qname, value);
      return;
    }

    ReferencePropertiesType eprPropsType = getReferenceProperties(eprType);
    XmlCursor propsCursor = eprPropsType.newCursor();
    propsCursor.push();

    propsCursor.toNextToken();
    propsCursor.insertAttributeWithValue(qname, value);

    propsCursor.pop();
    propsCursor.dispose();

  }



   /** Adds an element to the ReferenceProperties type within the specified
      Endpoint reference. If type does not contain the ReferenceProperties
      element, a new one is created. Then the specified element is added to
      the ReferenceProperties*/
  public void addElementToReferenceProperties(EndpointReferenceType eprType,
					      QName qname, String value) {
    if (qname == null || value == null) {
      reportOnQNameAndValue(qname, value);
      return;
    }

    ReferencePropertiesType eprPropsType = getReferenceProperties(eprType);
    XmlCursor propsCursor = eprPropsType.newCursor();
    propsCursor.push();

    propsCursor.toNextToken();
    propsCursor.insertElementWithText(qname, value);

    propsCursor.pop();
    propsCursor.dispose();

  }


  /** Copies the contents of the specified XmlObject into the reference
      properties element */
  public void addToReferenceProperties(EndpointReferenceType eprType,
				       XmlObject object) {
    if (object == null) {
      System.out.println(moduleName + "Specified XmlObject is NULL");
      return;
    }

    ReferencePropertiesType eprPropsType = getReferenceProperties(eprType);
    XmlCursor propsCursor = eprPropsType.newCursor();
    propsCursor.push();

    propsCursor.toNextToken();
    XmlCursor objectCursor = object.newCursor();
    objectCursor.copyXmlContents(propsCursor);
    objectCursor.dispose();

    propsCursor.pop();
    propsCursor.dispose();
  }




  /** Adds an attribute to the ReferenceParameters type within the specified
      Endpoint reference. If type does not contain the ReferenceParameters
      element, a new one is created. Then the specified attribute is added to
      the ReferenceParameters*/
  public void addReferenceParametersAttribute(EndpointReferenceType eprType,
					      QName qname, String value) {
    if (qname == null || value == null) {
      reportOnQNameAndValue(qname, value);
      return;
    }
    ReferenceParametersType eprParamsType = getReferenceParameters(eprType);

    XmlCursor paramsCursor = eprParamsType.newCursor();
    paramsCursor.push();

    paramsCursor.toNextToken();
    paramsCursor.insertAttributeWithValue(qname, value);

    paramsCursor.pop();
    paramsCursor.dispose();
  }



   /** Adds an element to the ReferenceParameters type within the specified
      Endpoint reference. If type does not contain the ReferenceParameters
      element, a new one is created. Then the specified element is added to
      the ReferenceParameters*/
  public void addElementToReferenceParameters(EndpointReferenceType eprType,
				       QName qname, String value) {
    if (qname == null || value == null) {
      reportOnQNameAndValue(qname, value);
      return;
    }


    ReferenceParametersType eprParamsType = getReferenceParameters(eprType);

    XmlCursor paramsCursor = eprParamsType.newCursor();
    paramsCursor.push();

    paramsCursor.toNextToken();
    paramsCursor.insertElementWithText(qname, value);

    paramsCursor.pop();
    paramsCursor.dispose();
  }



  /** Copies the contents of the specified XmlObject into the reference
      parameters element */
  public void addToReferenceParameters(EndpointReferenceType eprType,
				       XmlObject object) {
    if (object == null) {
      System.out.println(moduleName + "Specified XmlObject is NULL");
      return;
    }

    ReferenceParametersType eprParamsType = getReferenceParameters(eprType);
    XmlCursor paramsCursor = eprParamsType.newCursor();
    paramsCursor.push();

    paramsCursor.toNextToken();
    XmlCursor objectCursor = object.newCursor();
    objectCursor.copyXmlContents(paramsCursor);
    objectCursor.dispose();

    paramsCursor.pop();
    paramsCursor.dispose();
  }



  /** If type does not contain the ReferenceParameters element, a new one is
      created and returned.*/
  private ReferenceParametersType
  getReferenceParameters(EndpointReferenceType eprType) {

    boolean hasEprParams = eprType.isSetReferenceParameters();
    if (!hasEprParams) {
      eprType.addNewReferenceParameters();
    }

    ReferenceParametersType eprParamsType = eprType.getReferenceParameters();
    return eprParamsType;
  }



  /** If type does not contain the ReferenceProperties element, a new one is
      created and returned.*/
  private ReferencePropertiesType
  getReferenceProperties(EndpointReferenceType eprType) {

    boolean hasEprProps = eprType.isSetReferenceProperties();
    if (!hasEprProps) {
      eprType.addNewReferenceProperties();
    }

    ReferencePropertiesType eprPropsType = eprType.getReferenceProperties();
    return eprPropsType;
  }


  private void reportOnQNameAndValue(QName qname, String value) {
    System.out.println(moduleName + "There is a problem with the parameters " +
		       "for updating the WSA EPR. The specified \nqname = " +qname
		       + ", the specified value = " + value);
  }
}
